﻿using System;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace LibExtend
{
    public class Draw : IDisposable
    {
        public void Dispose()
        {
            _selectionBlend = null;
            _interpolationColors = null;
            GC.Collect();
        }

        #region Fields
        ButtonState _buttonState = ButtonState.None;
        ColorBlend _selectionBlend = null;
        Color[] _interpolationColors = null;
        Int32 DEF_PADDING = 10;
        Rectangle _bound = new Rectangle();
        #endregion

        internal Color[] InterpolationColors
        {
            get
            {
                if (_interpolationColors == null)
                {
                    _interpolationColors = new Color[] 
                    {
                        Color.FromArgb(255,226,119),
                        Color.FromArgb(255,227,124),
                        Color.FromArgb(255,229,133),
                        Color.FromArgb(255,238,164),
                        Color.FromArgb(255,244,192),
                        Color.FromArgb(255,250,214),
                        Color.FromArgb(255,252,224)
                    };
                }

                return _interpolationColors;
            }
        }

        internal ColorBlend SelectionBlend
        {
            get
            {
                if (_selectionBlend == null)
                {
                    _selectionBlend = new ColorBlend(7);
                    _selectionBlend.Positions = new float[]
                    {
                       0F,
                       0.83F,
                       0.86F,
                       0.9F,
                       0.93F,
                       0.96F,
                       1F
                    };
                }

                return _selectionBlend;
            }

        }

        #region Overrides
        public void OnPaint(PaintEventArgs pevent, Rectangle rect, ButtonState state)
        {
            _bound = rect;
            _buttonState = state;

            PaintButtonBackground(pevent);

            PaintButtonBorder(pevent);
        }
        #endregion

        private void PaintButtonBackground(PaintEventArgs e)
        {
            //ButtonState state = ButtonState.Selected;

            using (LinearGradientBrush brush = GetBackgroundGradientBrush(_buttonState))
            {
                e.Graphics.FillRectangle(brush, this._bound);
            }
        }

        private LinearGradientBrush GetBackgroundGradientBrush(ButtonState buttonState)
        {
            Color gradientBegin = Color.Empty;
            Color gradientEnd = Color.Empty;

            switch (buttonState)
            {
                case ButtonState.None:
                    gradientBegin = Color.FromArgb(163, 163, 163);
                    gradientEnd = Color.FromArgb(201, 201, 201);
                    break;
                case ButtonState.Pressed:
                    gradientBegin = Color.FromArgb(254, 226, 135);
                    gradientEnd = Color.FromArgb(251, 214, 120);
                    break;
                case ButtonState.Selected:
                    LinearGradientBrush brush = new LinearGradientBrush(this._bound, gradientEnd, gradientBegin, LinearGradientMode.Vertical);
                    this.SelectionBlend.Colors = this.InterpolationColors;
                    brush.InterpolationColors = this.SelectionBlend;
                    return brush;
            }
            return new LinearGradientBrush(this._bound, gradientEnd, gradientBegin, LinearGradientMode.Vertical);
        }

        private void PaintButtonBorder(PaintEventArgs e)
        {
            PaintButtonBorder(e, _buttonState);
        }

        private void PaintButtonBorder(PaintEventArgs e, ButtonState buttonState)
        {
            Rectangle border = this._bound;
            border.Inflate(0, 0);
            border.Size = new Size(this._bound.Width - 1, this._bound.Height - 1);

            switch (buttonState)
            {
                case ButtonState.Pressed:
                    {
                        using (Pen borderPen = new Pen(Color.FromArgb(194, 118, 43)))
                        {
                            e.Graphics.DrawRectangle(borderPen, border);
                        }
                    }
                    break;
                case ButtonState.Selected:
                    {
                        Rectangle innerLine = border;
                        innerLine.Inflate(-1, -1);
                        innerLine.Size = new Size(border.Width - 2, border.Height - 2);

                        using (Pen borderPen = new Pen(Color.FromArgb(248, 212, 39)))
                        {
                            e.Graphics.DrawRectangle(borderPen, border);
                        }

                        using (Pen borderPen = new Pen(Color.WhiteSmoke))
                        {
                            e.Graphics.DrawRectangle(borderPen, innerLine);
                        }
                    }
                    break;
            }
        }

        //protected ButtonState GetButtonState()
        //{
        //    Point mousePosition = this.PointToClient(Control.MousePosition);

        //    if (this.ClientRectangle.Contains(mousePosition))
        //    {
        //        if (Control.MouseButtons == MouseButtons.None)
        //            return ButtonState.Selected;
        //        else
        //            return ButtonState.Pressed;
        //    }

        //    return ButtonState.None;
        //}

    }

    #region Enum
    public enum ButtonState
    {
        None = 0,
        Pressed,
        Selected
    }
    #endregion
}
